package niconama04.client;

import java.util.List;

import niconama04.shared.data.Category;
import niconama04.shared.data.Original;
import niconama04.shared.data.Sentence;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;


public class Create implements EntryPoint {

	/** Create a remote service proxy to talk to the server-side service. */
	final CreateServiceAsync service = GWT.create(CreateService.class);

	Button btnO;

	Button btnC;

	Panel panel;

	Label label;

	ListBox originalListBox;

	ListBox categoryListBox;

	@Override
	public void onModuleLoad() {

		ViewBuilder.build();

		{
			originalListBox = new ListBox();
			originalListBox.addChangeHandler(new ChangeHandler() {
				
				@Override
				public void onChange(ChangeEvent x_event) {
					int index = originalListBox.getSelectedIndex();
					String value = originalListBox.getValue(index);
					if (value == null || value.equals("")) {
						return;
					}
					Long key = Long.valueOf(value);
					Original original = new Original();
					original.setKey(key);
					label.setText("");
					service.getSentenceByOriginal(original, new AsyncCallback<Sentence>() {

						@Override
						public void onFailure(Throwable x_caught) {
						}

						@Override
						public void onSuccess(Sentence x_result) {
							if (x_result != null) {
								label.setText(x_result.getSentence());
							}
						}
					});
				}
			});
			service.getOriginals(new AsyncCallback<List<Original>>() {
				
				@Override
				public void onFailure(Throwable x_caught) {
				}
				
				@Override
				public void onSuccess(List<Original> x_result) {
					originalListBox.addItem("", "");
					for (Original original : x_result) {
						originalListBox.addItem(original.getBaseform(), String.valueOf(original.getKey()));
					}
				}
			});
		}
		{
			categoryListBox = new ListBox();
			categoryListBox.addChangeHandler(new ChangeHandler() {
				
				@Override
				public void onChange(ChangeEvent x_event) {
					int index = categoryListBox.getSelectedIndex();
					String value = categoryListBox.getValue(index);
					if (value == null || value.equals("")) {
						return;
					}
					Long key = Long.valueOf(value);
					Category category = new Category();
					category.setKey(key);
					label.setText("");
					service.getSentenceByCategory(category, new AsyncCallback<Sentence>() {

						@Override
						public void onFailure(Throwable x_caught) {
						}

						@Override
						public void onSuccess(Sentence x_result) {
							if (x_result != null) {
								label.setText(x_result.getSentence());
							}
						}
					});
				}
			});
			service.getCategorys(new AsyncCallback<List<Category>>() {

				@Override
				public void onFailure(Throwable x_caught) {
				}

				@Override
				public void onSuccess(List<Category> x_result) {
					categoryListBox.addItem("", "");
					for (Category category : x_result) {
						categoryListBox.addItem(category.getName(), String.valueOf(category.getKey()));
					}
				}
			});
		}

		{
			RootPanel rp = RootPanel.get("gwt_create");
			if (rp == null) {
				return;
			}

			VerticalPanel vp = new VerticalPanel();
			{
				rp.add(vp);
			}
			{
				HorizontalPanel buttons = new HorizontalPanel();
				btnO = new Button("Original");
				btnC = new Button("Category");
				buttons.add(btnO);
				buttons.add(btnC);
				vp.add(buttons);
			}
			{
				VerticalPanel _vp = new VerticalPanel();
				panel = new FlowPanel();
				label = new Label();
				{
					Label sep = new Label("");
					sep.setHeight("10px");
					_vp.add(sep);
				}
				_vp.add(panel);
				{
					Label sep = new Label("");
					sep.setHeight("10px");
					_vp.add(sep);
				}
				_vp.add(label);
				vp.add(_vp);
			}
		}
		{
			btnO.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent x_event) {
					showOriginal();
				}
			});
			btnC.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent x_event) {
					showCategory();
				}
			});
		}
		{
			showOriginal();
		}
	}

	void showOriginal() {
		panel.clear();
		panel.add(originalListBox);
		label.setText("");
	}

	void showCategory() {
		panel.clear();
		panel.add(categoryListBox);
		label.setText("");
	}
}
